package com.zhang.network.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.dto.BaseResponse;
import com.zhang.network.constant.BizResponse;
import com.zhang.network.model.Network;
import com.zhang.network.model.network.NetWorkQueryDTO;
import com.zhang.network.service.NetworkService;
import com.zhang.network.service.NetworkUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 人脉圈管理前端控制器
 *
 * @author zhangshuaiyin
 * @since 2021-04-03
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/network")
public class NetworkController {

    @Resource
    NetworkService networkService;
    @Resource
    NetworkUserService networkUserService;

    /**
     * 分页获取/搜索我管理的人脉圈列表
     *
     * @param queryDTO
     * @return
     */
    @GetMapping("/page")
    public BaseResponse<Page<Network>> page(NetWorkQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<Network> networkPage = networkService.pageNetwork(queryDTO);
        return BizResponse.success(networkPage);
    }

    /**
     * 分页获取/搜索我所在的人脉圈列表
     *
     * @param queryDTO
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<Page<Network>> pageIn(NetWorkQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<Network> networkPage = networkService.pageInNetwork(queryDTO);
        return BizResponse.success(networkPage);
    }

    /**
     * 获取人脉圈类型列表
     *
     * @return
     */
    @GetMapping("/types")
    public BaseResponse<List<String>> getTypes() {
        return BizResponse.success(networkService.getNetworkTypes());
    }

    /**
     * 添加人脉圈基本信息
     *
     * @param network
     * @return
     */
    @PostMapping
    public BaseResponse<Network> insert(@RequestBody Network network) {
        if (networkService.insertNetwork(network)) {
            return BizResponse.success(network);
        }
        return BizResponse.fail();
    }

    /**
     * 人脉圈基本信息回显（根据id）
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public BaseResponse<Network> getById(@PathVariable("id") Integer id) {
        return BizResponse.success(networkService.getById(id));
    }


    /**
     * 修改人脉圈基本信息(标题、类型、话题、是否公开等)
     *
     * @param network
     * @return
     */
    @PutMapping
    public BaseResponse<Object> update(@RequestBody Network network) {
        if (networkService.modifyNetwork(network)) {
            return BizResponse.success();
        }
        return BizResponse.fail();
    }

    /**
     * 删除我的人脉圈
     *
     * @param id 人脉圈id
     * @return
     */
    @DeleteMapping("/{id}")
    public BaseResponse<Object> delete(@PathVariable("id") Integer id) {
        if (networkService.deleteNetwork(id)) {
            return BizResponse.success();
        }
        return BizResponse.fail();
    }
}

